package me.firstapp.api.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.HttpUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.system.Operator;
import me.firstapp.service.system.OperatorService;

@Controller
@RequestMapping(value = "/operator")
@Api(value = "/operator", description = "操作员相关接口")
public class OperatorApi extends BaseApi {

	@Autowired
	private OperatorService operatorService;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@RequestMapping(value = "findOperatorById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过id查询操作员", notes = "通过id查询操作员")
	public void findOperatorById(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.findOperatorById(id, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
	
	@RequestMapping(value = "findOperatorByName.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过账号查询操作员", notes = "通过账号查询操作员")
	public void findOperatorByName(String name, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.findOperatorByName(name, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addOperator.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增操作员", notes = "新增操作员")
	public void addOperator(String email, String password, String name, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.addOperator(email, password, name, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "login.do", method = RequestMethod.POST)
	@ApiOperation(value = "操作员登录", notes = "操作员登录")
	public void login(String name, String password, String code, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.findOperatorByName(name, apiClientId);
			if(operator == null){
				throw new ServiceException(StatusHouse.OPERATOR_NOT_EXIST);
			}
			verifyCode(stringRedisTemplate, code, operator.getEmail());
			Operator _operator = operatorService.executeLogin(name, password, HttpUtils.getIpAddress(request),
					apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(_operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findOperatorPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询操作员", notes = "分页查询操作员")
	public void findOperatorPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Operator> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Operator> configPage = operatorService.findOperatorPage(pageNo, pageSize, searchParams, sortParam,
					sortType, apiClientId);
			JsonPage<Operator> jsonPage = new JsonPage<Operator>();
			jsonPage.setList(configPage.getEntities());
			jsonPage.setPageNo(configPage.getPageNo());
			jsonPage.setPageSize(configPage.getPageSize());
			jsonPage.setTotalCount(configPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "alterPassword.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改操作员密码", notes = "修改操作员密码")
	public void alterPassword(Long id, String oldPassword, String newPassword, String code, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.findOperatorById(id, apiClientId);
			verifyCode(stringRedisTemplate, code, operator.getEmail());
			operator = operatorService.executeAlterPassword(id, oldPassword, newPassword, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateStatus.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改操作员状态", notes = "修改操作员状态")
	public void updateStatus(Long id, Integer status, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Operator> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Operator operator = operatorService.updateStatus(id, status, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(operator);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
